package com.ds.infrastructure.hyperspace.console.controller;

import com.ds.infrastructure.hyperspace.console.entity.page.TbPageWhere;
import com.ds.infrastructure.hyperspace.console.entity.table.TbHandler;
import com.ds.infrastructure.hyperspace.console.exceptions.HyperspaceException;
import com.ds.infrastructure.hyperspace.console.model.request.form.PageForm;
import com.ds.infrastructure.hyperspace.console.model.request.handler.ListHandlerForm;
import com.ds.infrastructure.hyperspace.console.model.request.handler.SaveHandlerForm;
import com.ds.infrastructure.hyperspace.console.model.response.handler.GetHandlerVO;
import com.ds.infrastructure.hyperspace.console.model.response.result.JsonResultData;
import com.ds.infrastructure.hyperspace.console.model.response.result.JsonResultList;
import com.ds.infrastructure.hyperspace.console.service.inf.IHandlerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * @author: yaozhendong
 * @create: 2019-09-20 19:45
 **/

@RestController
@RequestMapping(value = "/handler", produces = {"application/json;charset=UTF-8"})
@Api(value = "Manager Handler Controller", tags = "Handler管理")
public class HandlerController {
    @Autowired
    IHandlerService iHandlerService;

    @PostMapping(value = "/save")
    @ApiOperation(value = "保存Handler")
    public JsonResultData<Long> save(@RequestBody @Valid SaveHandlerForm form) {
        TbHandler item = iHandlerService.selectOne(SaveHandlerForm.toEntity4SelectOne(form), null);
        if (item != null) {
            throw new HyperspaceException("保存Handler失败，Handler已经存在，handlerName=" + form.getHandlerName() + ";handlerType=" + form.getHandlerType());
        }
        TbHandler entity = SaveHandlerForm.toEntity(form);
        iHandlerService.save(entity);
        return JsonResultData.build(entity.getId());
    }

    @PostMapping(value = "/list")
    @ApiOperation(value = "Handler列表")
    public JsonResultList<GetHandlerVO> list(@RequestBody @Valid ListHandlerForm form) {
        TbHandler where = ListHandlerForm.toEntity(form);
        TbPageWhere pageWhere = PageForm.toPageWhere(form);
        int dataTotal = iHandlerService.getTotal(where, null);
        if (dataTotal == 0 || pageWhere.getBegin() > dataTotal) {
            return JsonResultList.build(Collections.emptyList(), dataTotal);
        }
        List<TbHandler> tbHandlerList = iHandlerService.getList(where, null, pageWhere);
        List<GetHandlerVO> dataList = new ArrayList<>();
        tbHandlerList.forEach(item -> dataList.add(GetHandlerVO.toVO(item)));
        return JsonResultList.build(dataList, dataTotal);
    }

    @GetMapping(value = "/get")
    @ApiOperation(value = "查看Handler")
    public JsonResultData<GetHandlerVO> get(@RequestParam(name = "id") Long id) {
        TbHandler entity = iHandlerService.get(id);
        if (entity == null) {
            throw new HyperspaceException("查看Handler失败，没有找到对应的记录;当前id=" + id);
        }
        GetHandlerVO result = GetHandlerVO.toVO(entity);
        return JsonResultData.build(result);
    }
}
